﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using BusinessObjects;
using DataAccess;

namespace QuanLyHopDong
{
    public partial class BacLuongDetailView : Form
    {
        public event EventHandler SaveChanged;
        public event EventHandler DeleteChanged;

        private BindingList<BacLuong> bindingListBacLuong;
        private BindingList<NgachLuong> bindingListNgachLuong;
        private BacLuong _bacLuong;
        private NgachLuong _ngachLuong;

        public BacLuongDetailView()
        {
            InitializeComponent();
        }
        public BacLuongDetailView(BacLuong bacLuong)
        {
            InitializeComponent();
            
            _bacLuong = bacLuong;
            bindingListBacLuong = new BindingList<BacLuong>();
            bindingListBacLuong.Add(_bacLuong);

            bindingListNgachLuong = new BindingList<NgachLuong>(DataAccessObject.NgachLuong.GetListNgachLuong());

            string[] listFieldName = new string[] { "MaQuanLy", "TenNgachLuong" };
            string[] listColnumName = new string[] { "Mã Quản Lý", "Tên Ngạch Lương" };
            AppGridView.InitGridView(glkViewNgachLuong, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, false);
            AppGridView.ReadOnlyGridView(glkViewNgachLuong);
            AppGridView.ShowField(glkViewNgachLuong, listFieldName, listColnumName);

            bindingSourceBacLuong.DataSource = bindingListBacLuong;
            bindingSourceNgachLuong.DataSource = bindingListNgachLuong;

            if (_bacLuong != null && _bacLuong.NgachLuong != null)
                glkNgachLuong.EditValue = _bacLuong.NgachLuong.Oid;
            layoutControl1.Refresh();
        }

        private void BacLuongDetailView_Load(object sender, EventArgs e)
        {
        }
        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Luu();
        }
        private void LuuVaDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Luu();
            this.Close();
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
        }

        void Luu()
        {
            if (_bacLuong.Oid == -1)
            {
                if (DataAccessObject.BacLuong.Insert(_bacLuong) > 0)
                    OnSaveChange();
            }
            else
                DataAccessObject.BacLuong.Update(_bacLuong);
        }

        private void glkNgachLuong_EditValueChanged(object sender, EventArgs e)
        {
            if (glkViewNgachLuong.GetFocusedRow() != null)
                _bacLuong.NgachLuong = glkViewNgachLuong.GetFocusedRow() as NgachLuong;
        }

        void OnDeleteChange()
        {
            if (DeleteChanged!= null)
                DeleteChanged(this, EventArgs.Empty);
        }
        void OnSaveChange()
        {
            if (SaveChanged!=null)
                SaveChanged(this, EventArgs.Empty);
        }
    }
}
